﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmThietLapChoNam_GiangVien : FormBase
    {
        public frmThietLapChoNam_GiangVien()
        {
            InitializeComponent();
        }

        public int NamThucTapID { get; set; }
        private DataTable _dtLoad;

        private void frmThietLapChoNam_GiangVien_Load(object sender, EventArgs e)
        {
            try
            {
                _dtLoad = new DataTable();
                _dtLoad.Columns.Add("Choose", typeof(bool));
                _dtLoad.Columns.Add("GiangVienID", typeof(int));
                _dtLoad.Columns.Add("MaGiangVien", typeof(string));
                _dtLoad.Columns.Add("Ho", typeof(string));
                _dtLoad.Columns.Add("Ten", typeof(string));
                _dtLoad.Columns.Add("TenKhoa", typeof(string));
                _dtLoad.Columns.Add("DienThoai", typeof(string));
                _dtLoad.Columns.Add("Email", typeof(string));
                LoadCongTy();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                DataRow[] drs = _dtLoad.Select("Choose=1");
                if (drs.Length > 0)
                {
                    if (CommonConfirm("Bạn muốn lưu dữ liệu này?", "LUU"))
                    {
                        using (NestedUnitOfWork nuow = Session.DefaultSession.BeginNestedUnitOfWork())
                        {
                            NamThucTap objNamThucTap = nuow.Query<NamThucTap>().FirstOrDefault(c => c.Oid == NamThucTapID);
                            if (objNamThucTap != null)
                            {
                                for (int i = 0; i < drs.Length; i++)
                                {
                                    DataRow dr = drs[i];
                                    int giangvienID = Convert.ToInt32(dr["GiangVienID"]);
                                    var objGiangVien = nuow.Query<GiangVien>().FirstOrDefault(c => c.Oid == giangvienID);
                                    if (objGiangVien != null)
                                    {
                                        GiangVienTrongNam obj = new GiangVienTrongNam(nuow);
                                        obj.NamThucTap = objNamThucTap;
                                        obj.GiangVien = objGiangVien;
                                    }
                                }
                                nuow.CommitChanges();
                            }
                        }
                        CommonMessage("Cập nhật thành công");
                        this.DialogResult = DialogResult.OK;
                    }
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void LoadCongTy()
        {
            var query = Session.DefaultSession.Query<GiangVien>().Where(c => c.GiangVienTrongNams.Count() == 0 || c.GiangVienTrongNams.Any(d => d.NamThucTap.Oid != NamThucTapID)).Select(c => new
            {
                c.Oid,
                c.MaGiangVien,
                c.HoGiangVien,
                c.TenGiangVien,
                c.Khoa.TenKhoa,
                c.DienThoai,
                c.Email
            });
            foreach (var item in query)
            {
                DataRow dr = _dtLoad.NewRow();
                dr["Choose"] = false;
                dr["GiangVienID"] = item.Oid;
                dr["MaGiangVien"] = item.MaGiangVien;
                dr["Ho"] = item.HoGiangVien;
                dr["Ten"] = item.TenGiangVien;
                dr["TenKhoa"] = item.TenKhoa;
                dr["DienThoai"] = item.DienThoai;
                dr["Email"] = item.Email;
                _dtLoad.Rows.Add(dr);
            }
            gvData.DataSource = _dtLoad;
        }

        private void chkChonTatCa_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                foreach (DataRow dr in _dtLoad.Rows)
                {
                    dr["Choose"] = Convert.ToBoolean(chkChonTatCa.EditValue);
                }
                gvData.DataSource = _dtLoad;
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void chkChonTatCa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }
    }
}